/**
 * Created by xuxujunjunforever on 2015/12/19.
 */
'use strict'

let models = require('../models');
let config = require('../../config');

let RoleDao = models.RoleDao;
let PrivilegeDao = models.PrivilegeDao;

let Privilege = models.Privilege;
let Role = models.Role;

let actionCreateRole = {
    privileges:['manage_role'],
    fields:{
        role:{
            name: true,
            privileges:{
                $type:'Array',
                id:true
            }
        }
    },
    handler(req,res) {
        if ( !req.session.login ) {
            throw config.errors.UserNotLogin;
            return;
        }

        let role = new Role(req.body.role);

        role.privileges.forEach(privilege=>{
            let findprivilege = PrivilegeDao.findOne({
                id:privilege.id
            });
            if(findprivilege == null){
                throw config.errors.PrivilegeCanNotFound;
                return;
            }
        });

        RoleDao.create(role);

        let roleInfomation = role.toObject({recursive:true});

        res.json({
            successful:true,
            data:{
                role:roleInfomation
            }
        });
    }
};

let actionRefreshRole = {
    privileges:['manage_role'],
    fields:{
        role:{
            id:true
        }
    },
    handler(req, res) {
        if ( !req.session.login ) {
            throw config.errors.UserNotLogin;
            return;
        }

        let role = req.body.role;

        let refreshRole = RoleDao.findOne({
            id:role.id
        });
        if(refreshRole == null){
            throw config.errors.RoleCanNotFound;
            return;
        }

        let roleInfomation = refreshRole.toObject({recursive:true});

        res.json({
            successful:true,
            data:{
                role:roleInfomation
            }
        });
    }
};

let actionUpdateRole = {
    handler(req,res) {
        if ( !req.session.login ) {
            throw config.errors.UserNotLogin;
            return;
        }

        let newrole = req.body.role;

        let oldrole = RoleDao.findOne({
            id:newrole.id
        });
        if(oldrole == null){
            throw config.errors.RoleCanNotFound;
            return;
        }

        newrole.privileges.forEach(privilege=>{
            let findprivilege = PrivilegeDao.findOne({
                id:privilege.id
            });
            if(findprivilege == null){
                throw config.errors.PrivilegeCanNotFound;
                return;
            }
        });

        RoleDao.update(newrole);

        let roleInfomation = newrole.toObject({recursive:true});

        res.json({
            successful:true,
            data:{
                role:roleInfomation
            }
        });
    }
};

let actionRemoveRole = {
    privileges:['manage_role'],
    fields:{
        role:{
            id:true
        }
    },
    handler(req, res) {
        if ( !req.session.login ) {
            throw config.errors.UserNotLogin;
            return;
        }

        let role = RoleDao.findOne({
            id:req.body.role.id
        });
        if(role == null){
            throw config.errors.RoleCanNotFound;
            return;
        }

        RoleDao.remove(role);

        res.json({
            successful:true
        });
    }
};

let actionRoleAll = {
    privileges:['manage_role'],
    handler(req, res) {
        if ( !req.session.login ) {
            throw config.errors.UserNotLogin;
            return;
        }

        let roles = RoleDao.find({});
        let plainroles = Role.toObjectArray(roles,{recursive:true});

        res.json({
            successful:true,
            data:{
                roles:plainroles
            }
        });
    }
};

module.exports = {
    actionCreateRole,
    actionRefreshRole,
    //actionUpdateRole,
    actionRemoveRole,
    actionRoleAll
};
